package com.zs.leymovie.adapter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zs.leymovie.R;

import java.util.List;

/**
 * Created by Administrator on 2015/8/8 0008.
 */
public class ShowImageGvAdapter extends AppAdapter {
    private List<String> list;
    private Context context;
    private BitmapDisplayConfig config;
    private final Animation animation;
    private BitmapUtils bitmapUtils;

    public ShowImageGvAdapter(List list, Context context) {
        super(list, context);
        this.list = list;
        this.context = context;
        config = new BitmapDisplayConfig();
        config.setLoadFailedDrawable(context.getResources().getDrawable(R.mipmap.ic_launcher));
        config.setLoadingDrawable(context.getResources().getDrawable(R.mipmap.ic_launcher));
        animation = AnimationUtils.loadAnimation(
                context, R.anim.home_listview_anim);
        config.setAnimation(animation);
        bitmapUtils = new BitmapUtils(context);
    }

    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.showimage_gv_layout, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        DisplayMetrics dm = new DisplayMetrics();
        //取得窗口属性
        WindowManager mWm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        mWm.getDefaultDisplay().getMetrics(dm);
        int width =  dm.widthPixels/5;
        vh.img.setLayoutParams(new LinearLayout.LayoutParams(width,width));
        bitmapUtils.display(vh.img, list.get(position), config);
        vh.img.setScaleType(ImageView.ScaleType.FIT_XY);
        return convertView;
    }

    public class ViewHolder {
        @ViewInject(R.id.category_gv_item_img)
        private ImageView img;

        public ViewHolder(View convertView) {
            ViewUtils.inject(this, convertView);
        }
    }
}
